#include "Material.h"
#include "../ResourceManager.h"

using namespace Stray;
using namespace Stray::Core;
using namespace Stray::Core::Graphics;
using namespace Stray::Resource;

const char Material::type_[] = "material";

Material::Material(const ResourceID& id) :
Resource(id), effectID_("","","","")
{
}

MaterialParamPtr Material::getParam(const String& semantic, Uint32 index)
{
	ParamMap::iterator iter = paramMap_.find(semantic);
	if(iter == paramMap_.end()) return MaterialParamPtr();

	if(index >= paramMap_[semantic].size()) return MaterialParamPtr();

	return paramMap_[semantic][index];
}

void Material::build(const MaterialCreationParam& param)
{
	ResourceManagerImpl& mgr = ResourceManager::getInstance();

	MaterialCreationParam::Parameters::const_iterator iter;
	for(iter = param.parameters_.begin(); iter != param.parameters_.end(); ++iter)
	{
		if((*iter)->index_ >= paramMap_[(*iter)->semantic_].size())
			paramMap_[(*iter)->semantic_].resize((*iter)->index_ + 1);

		MaterialParamPtr spParam = paramMap_[(*iter)->semantic_][(*iter)->index_];
		
		spParam->type_ = (*iter)->value_.type_;
		spParam->value_ = (*iter)->value_.value_;
		spParam->textureID_ = (*iter)->value_.textureId_;
	}
}
